
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Pubsub · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Code.html" />
    
    
    <link rel="prev" href="Testing.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    What is ThingsJS?
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../about/Architecture.html">
            
                <a href="../about/Architecture.html">
            
                    
                    System Architecture
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../about/Terminology.html">
            
                <a href="../about/Terminology.html">
            
                    
                    Terminology
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="CLI.html">
            
                <a href="CLI.html">
            
                    
                    CLI Commands
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="../Publications.html">
            
                <a href="../Publications.html">
            
                    
                    Publications
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../GettingStarted.html">
            
                <a href="../GettingStarted.html">
            
                    
                    Getting Started
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../tutorials/VirtualMachine.html">
            
                <a href="../tutorials/VirtualMachine.html">
            
                    
                    ThingsJS VM Image
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../tutorials/Setup-PocketBeagle.html">
            
                <a href="../tutorials/Setup-PocketBeagle.html">
            
                    
                    Pocket Beagle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../tutorials/Setup-BeagleboneBlack.html">
            
                <a href="../tutorials/Setup-BeagleboneBlack.html">
            
                    
                    Beaglebone Black
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../samples/">
            
                <a href="../samples/">
            
                    
                    Sample Components
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../samples/RIoTBenchmarks.html">
            
                <a href="../samples/RIoTBenchmarks.html">
            
                    
                    Sample Benchmarks
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../tutorials/">
            
                <a href="../tutorials/">
            
                    
                    Tutorials
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../tutorials/FirstApplication.html">
            
                <a href="../tutorials/FirstApplication.html">
            
                    
                    Writing your First App
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../tutorials/Migration.html">
            
                <a href="../tutorials/Migration.html">
            
                    
                    Live-Migration
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../tutorials/dashboard/Dashboard.html">
            
                <a href="../tutorials/dashboard/Dashboard.html">
            
                    
                    Using the Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="./">
            
                <a href="./">
            
                    
                    Developer API
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="Devdocs.html">
            
                <a href="Devdocs.html">
            
                    
                    Developer Documentation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="Testing.html">
            
                <a href="Testing.html">
            
                    
                    Running Unit Tests
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.3" data-path="Pubsub.html">
            
                <a href="Pubsub.html">
            
                    
                    Pubsub
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Code.html">
            
                <a href="Code.html">
            
                    
                    Code
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="CodeEngine.html">
            
                <a href="CodeEngine.html">
            
                    
                    CodeEngine
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="Dispatcher.html">
            
                <a href="Dispatcher.html">
            
                    
                    Dispatcher
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="Scheduler.html">
            
                <a href="Scheduler.html">
            
                    
                    Scheduler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="GFS.html">
            
                <a href="GFS.html">
            
                    
                    File system
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="Dashboard.html">
            
                <a href="Dashboard.html">
            
                    
                    Web Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../Community/">
            
                <a href="../Community/">
            
                    
                    Community
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Community/involve.html">
            
                <a href="../Community/involve.html">
            
                    
                    Get involved
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Community/contribute.html">
            
                <a href="../Community/contribute.html">
            
                    
                    Contribute
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Community/feedback.html">
            
                <a href="../Community/feedback.html">
            
                    
                    Feedback
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Community/team.html">
            
                <a href="../Community/team.html">
            
                    
                    Team
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Pubsub</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="classes">Classes</h2>
<dl>
<dt><a href="#Pubsub">Pubsub</a></dt>
<dd></dd>
<dt><a href="#DummyPubsub">DummyPubsub</a></dt>
<dd></dd>
<dt><a href="#PubsubServer">PubsubServer</a></dt>
<dd></dd>
</dl>

<p><a name="Pubsub"></a></p>
<h2 id="pubsub">Pubsub</h2>
<p><strong>Kind</strong>: global class  </p>
<ul>
<li><a href="#Pubsub">Pubsub</a><ul>
<li><a href="#new_Pubsub_new">new Pubsub(url, options)</a></li>
<li><em>instance</em><ul>
<li><a href="#Pubsub+subscribe">.subscribe(topic, handler)</a> &#x21D2; <code>Promise</code></li>
<li><a href="#Pubsub+unsubscribe">.unsubscribe(topic)</a> &#x21D2; <code>Promise</code></li>
<li><a href="#Pubsub+publish">.publish(topic, message)</a> &#x21D2; <code>Promise</code></li>
<li><a href="#Pubsub+kill">.kill()</a> &#x21D2; <code>Promise</code></li>
</ul>
</li>
<li><em>static</em><ul>
<li><a href="#Pubsub.Server">.Server</a></li>
<li><a href="#Pubsub.Dummy">.Dummy</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<p><a name="new_Pubsub_new"></a></p>
<h3 id="new-pubsuburl-options">new Pubsub(url, options)</h3>
<p>Pubsub object provides client-side interface for talking to a MQTT Publish/Subscribe Server.
It is a thin wrapper around the <a href="https://github.com/mqttjs/MQTT.js" target="_blank">mqtt</a> client</p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>url</td>
<td><code>string</code></td>
<td>The URL of the Pub/Sub service</td>
</tr>
<tr>
<td>options</td>
<td><code>object</code></td>
<td>additional options object</td>
</tr>
<tr>
<td>options.handlers</td>
<td><code>object</code></td>
<td>a dictionary of <topic>:<function> for initializing subscriptions</function></topic></td>
</tr>
</tbody>
</table>
<p><a name="Pubsub+subscribe"></a></p>
<h3 id="pubsubsubscribetopic-handler-&#x21D2;-promise">pubsub.subscribe(topic, handler) &#x21D2; <code>Promise</code></h3>
<p>Subscribe to a given topic, passing in a callback to invoke upon reception of the message under the said topic.</p>
<p><strong>Kind</strong>: instance method of <a href="#Pubsub"><code>Pubsub</code></a><br><strong>Returns</strong>: <code>Promise</code> - - On successful subscription, the Promise resolves to the topic.  </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>topic</td>
<td><code>String</code></td>
<td>the topic to subscribe to</td>
</tr>
<tr>
<td>handler</td>
<td><code>function</code></td>
<td>the callback function to invoke when a message arrives under the given topic. The callback must have the signature <code>function(message, topic, rawBuffer)</code></td>
</tr>
</tbody>
</table>
<p><a name="Pubsub+unsubscribe"></a></p>
<h3 id="pubsubunsubscribetopic-&#x21D2;-promise">pubsub.unsubscribe(topic) &#x21D2; <code>Promise</code></h3>
<p>Unsubscribe from a given topic. If the client is not subscribed, it will immediately resolve and will not reject with an error</p>
<p><strong>Kind</strong>: instance method of <a href="#Pubsub"><code>Pubsub</code></a><br><strong>Returns</strong>: <code>Promise</code> - - the Promise always resolves to the topic regardless of the outcome.  </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>topic</td>
<td><code>String</code></td>
<td>the topic to unsubscribe from</td>
</tr>
</tbody>
</table>
<p><a name="Pubsub+publish"></a></p>
<h3 id="pubsubpublishtopic-message-&#x21D2;-promise">pubsub.publish(topic, message) &#x21D2; <code>Promise</code></h3>
<p>Publish to a given topic. If the given message is not a Buffer object, it will try and JSON.stringify it.</p>
<p><strong>Kind</strong>: instance method of <a href="#Pubsub"><code>Pubsub</code></a><br><strong>Returns</strong>: <code>Promise</code> - - resolves to through on successful publish  </p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>topic</td>
<td><code>String</code></td>
<td>the topic to publish to</td>
</tr>
<tr>
<td>message</td>
<td><code>Object</code> \</td>
<td><code>Buffer</code></td>
<td>message to send (assumed to be a JSON-serializable object by default)</td>
</tr>
</tbody>
</table>
<p><a name="Pubsub+kill"></a></p>
<h3 id="pubsubkill-&#x21D2;-promise">pubsub.kill() &#x21D2; <code>Promise</code></h3>
<p>Kill the Pubsub instance, closing the connection.</p>
<p><strong>Kind</strong>: instance method of <a href="#Pubsub"><code>Pubsub</code></a><br><strong>Returns</strong>: <code>Promise</code> - - the Promise always resolves true regardless of the outcome.<br><a name="Pubsub.Server"></a></p>
<h3 id="pubsubserver">Pubsub.Server</h3>
<p>PubsubServer is available as a property of Pubsub</p>
<p><strong>Kind</strong>: static property of <a href="#Pubsub"><code>Pubsub</code></a><br><a name="Pubsub.Dummy"></a></p>
<h3 id="pubsubdummy">Pubsub.Dummy</h3>
<p>DummyPubsub is available as a property of Pubsub</p>
<p><strong>Kind</strong>: static property of <a href="#Pubsub"><code>Pubsub</code></a><br><a name="DummyPubsub"></a></p>
<h2 id="dummypubsub">DummyPubsub</h2>
<p><strong>Kind</strong>: global class<br><a name="new_DummyPubsub_new"></a></p>
<h3 id="new-dummypubsub">new DummyPubsub()</h3>
<p>Dummy Pubsub used for instrumenting code without connecting.</p>
<ul>
<li>does not extend Pubsub (i.e. not a subclass), this is a standalone definition</li>
<li>this object is created instead of actual Pubsub when user code is initialized as a Process via Code.run().</li>
<li>when user code is executed via Code.run(), it has an IPC channel to the master process.
in that case, pubsub.publish is done by the master process, and the child process (this process) sends
the message to the master process.</li>
</ul>
<p><a name="PubsubServer"></a></p>
<h2 id="pubsubserver">PubsubServer</h2>
<p><strong>Kind</strong>: global class  </p>
<ul>
<li><a href="#PubsubServer">PubsubServer</a><ul>
<li><a href="#new_PubsubServer_new">new PubsubServer()</a></li>
<li><a href="#PubsubServer+kill">.kill()</a> &#x21D2; <code>Promise</code></li>
</ul>
</li>
</ul>
<p><a name="new_PubsubServer_new"></a></p>
<h3 id="new-pubsubserver">new PubsubServer()</h3>
<p>PubsubServer is a thin wrapper around the Mosca Server</p>
<p><a name="PubsubServer+kill"></a></p>
<h3 id="pubsubserverkill-&#x21D2;-promise">pubsubServer.kill() &#x21D2; <code>Promise</code></h3>
<p>Kill the PubsubServer</p>
<p><strong>Kind</strong>: instance method of <a href="#PubsubServer"><code>PubsubServer</code></a>  </p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Testing.html" class="navigation navigation-prev " aria-label="Previous page: Running Unit Tests">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Code.html" class="navigation navigation-next " aria-label="Next page: Code">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Pubsub","level":"1.4.3","depth":2,"next":{"title":"Code","level":"1.4.4","depth":2,"path":"api/Code.md","ref":"api/Code.md","articles":[]},"previous":{"title":"Running Unit Tests","level":"1.4.2","depth":2,"path":"api/Testing.md","ref":"api/Testing.md","articles":[]},"dir":"ltr"},"config":{"plugins":["jsdoc"],"root":"./guide","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"jsdoc":{"options":"{}"},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"gitbook":"*"},"file":{"path":"api/Pubsub.md","mtime":"2018-10-15T23:34:02.479Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-04-24T23:23:12.622Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

